//引入第三方express模块
const express=require("express");
//引入第三方路由器模块
const router=express.Router();
//引入连接池
const pool=require("../pool")

//创建路由，端口
router.get("/",(req,res)=>{
  var sql="select * from cardrule"
  pool.query(sql,[],(err,result)=>{
    if(err) console.log(err);
    res.send(result)
  })
})

router.post("/add",(req,res)=>{
  console.log(req.body)
  var $cardClass=req.body.cardClass;
  var $cardTitle=req.body.cardTitle;
  var $cardContent=req.body.cardContent;


  var sql="INSERT INTO cardrule VALUES(null,?,?,?)";
  pool.query(sql,[$cardClass,$cardTitle,$cardContent],(err,result)=>{
    if(err) throw err;
    if(result.affectedRows>0){
      res.send('200');
    }
  })
})


router.get("/del",(req,res)=>{
  console.log(req.query)
  var $cardId=req.query.cardId
  var sql="delete from cardrule where cardId=?";
  pool.query(sql,[$cardId],(err,result)=>{
    if(err) throw err;
    if(result.affectedRows>0){
      res.send('200');
    }
  })
});


router.post("/update",(req,res)=>{
  console.log(req.body)
  var $cardId=req.body.cardId
  var $cardClass=req.body.cardClass;
  var $cardTitle=req.body.cardTitle;
  var $cardContent=req.body.cardContent;
  //console.log($pid)
  var sql="update cardrule set cardClass=?,cardTitle=?,cardContent=? where cardId=?"
  pool.query(sql,[$cardClass,$cardTitle,$cardContent,$cardId],(err,result)=>{
    if(err) throw err;
    if(result.affectedRows>0){
      res.send('200');
    }
  })
})

// router.post('/update',(req,res)=>{
//   var result=req.body;
//   var uid=result.uid;
//   var uname=result.uname;
//   var upwd=result.upwd;
//   var uphone=result.uphone;
//   var uemail=result.uemail;
//   console.log(uid,uname,upwd,uphone,uemail);
// })
//公开模块
module.exports=router;